<!--
 * @Author: your name
 * @Date: 2021-07-08 14:01:21
 * @LastEditTime: 2021-07-09 10:28:13
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \yuyunbook\src\views\login\index.vue
-->
<template>
  <div class="login-container">
    <div class="login-main">
      <div class="login-title">管理系统</div>
      <el-form ref="login" class="login-content" :rules="rules" :model="userInfo">
        <el-form-item prop="username">
          <el-input v-model="userInfo.username" placeholder="账号">
            <template #prepend>
              <el-button icon="el-icon-user"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="userInfo.password" >
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
        </el-form-item>

        <div class="login-btn">
            <el-button type="primary" @click="handleSignIn">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

// 用户登录
const useSignIn = () => {
  const userInfo = reactive({
    username: 'admin',
    password: '123123'
  })

  const rules = {
    username: [
      { required: true, message: 'please enter user name', trigger: 'blur' }
    ],
    password: [
      { required: true, message: 'please enter password', trigger: 'blur' }
    ]
  }

  const login:any = ref(null)
  const router = useRouter()

  const handleSignIn = () => {
    console.log(login)
    login.value.validate((valid:Boolean) => {
      if (valid) {
        ElMessage.success('成功')

        router.replace('/')
      } else {
        console.log('失败')
      }
    })
  }

  return {
    userInfo,
    rules,
    login,
    handleSignIn
  }
}

export default {
  name: 'Login',

  setup() {
    return {
      ...useSignIn()
    }
  }
}

</script>

<style scoped lang="scss">
.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background-image: url(https://file.iviewui.com/iview-admin/login_bg.jpg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-main {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}

.login-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.login-content {
  padding: 30px 30px;
}

.login-btn {
    text-align: center;
}

.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
</style>
